<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="请购单号" prop="vBillNo">
        <el-input
          v-model="queryParams.vBillNo"
          placeholder="请输入请购单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人" prop="applyBy">
        <el-input
          v-model="queryParams.applyBy"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="请购日期">
        <el-date-picker
          v-model="daterangeDRequest"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="部门号" prop="vPartNo">
        <el-input
          v-model="queryParams.vPartNo"
          placeholder="请输入部门号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleImport"
          >导入请购单</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="billList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="部门流水号" align="center" prop="vPartNo" />
      <el-table-column label="请购单编号" align="center" prop="vBillNo" />
      <el-table-column
        label="请购日期"
        align="center"
        prop="dRequest"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dRequest, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="applyBy" />
      <el-table-column label="单据状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.buy_requestbill_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >提交</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改请购单对话框 -->
    <el-drawer
      :title="title"
      :before-close="handleClose"
      :visible.sync="open"
      size="75%"
      ref="drawer"
    >
      <div class="drawer-container">
        <div class="xin-main">
          <div class="xin-content" style="height: 90%">
            <el-form
              ref="form"
              :model="form"
              :rules="rules"
              label-width="100px"
              disabled="true"
            >
              <el-row>
                <el-col :span="6">
                  <el-form-item label="请购单编号" prop="vBillNo">
                    <el-input
                      v-model="form.vBillNo"
                      placeholder="请输入请购单编号"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="请购日期" prop="dRequest">
                    <el-date-picker
                      clearable
                      v-model="form.dRequest"
                      type="date"
                      value-format="yyyy-MM-dd"
                      placeholder="请选择请购日期"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="部门流水号" prop="vPartNo">
                    <el-input
                      v-model="form.vPartNo"
                      placeholder="请输入部门流水号"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" />
                  </el-form-item> </el-col
              ></el-row>
            </el-form>
            <el-divider content-position="left"></el-divider>
            <el-table :data="itemList" height="450px">
              <el-table-column
                label="序号"
                align="center"
                type="index"
                prop="index"
              />
              <el-table-column
                label="项目号"
                align="center"
                prop="vProjectNo"
              />
              <el-table-column
                label="项目简称"
                align="center"
                prop="vProjectDec"
              />
              <el-table-column
                label="零件名称"
                align="center"
                prop="vSpaName"
              />
              <el-table-column
                label="规格型号"
                align="center"
                prop="vSpaModel"
                width="200px"
              />
              <el-table-column
                label="推荐品牌"
                align="center"
                prop="vSpaBrand"
              />
              <el-table-column label="数量" align="center" prop="nQty" />
              <el-table-column label="单位" align="center" prop="vUnit" />
              <el-table-column label="要求货期" align="center" prop="nDays" />
              <el-table-column label="特殊说明" align="center" prop="remark" />
            </el-table>
            <div
              class="xin-footer"
              style="position: fixed; bottom: 5px; right: 10px"
            >
              <el-button type="primary" @click="submitForm('02')"
                >确 定</el-button
              >
              <el-button @click="cancel()">取 消</el-button>
            </div>
          </div>
        </div>
      </div>
    </el-drawer>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="500px">
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-link type="info" style="font-size: 12px" @click="importTemplate"
            >下载模板</el-link
          >
        </div>
        <div class="el-upload__tip" style="color: red" slot="tip">
          提示：仅允许导入“xls”或“xlsx”格式文件！
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBill,
  getBill,
  delBill,
  addBill,
  updateBill,
} from "@/api/buy/buy0008";

import { getToken } from "@/utils/auth";

export default {
  dicts: ["buy_requestbill_status"],
  name: "buy0008",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请购单表格数据
      billList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 部门流水号时间范围
      daterangeDRequest: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vBillNo: null,
        applyBy: null,
        dRequest: null,
        vPartNo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      itemList: [],
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/buy/buyRequestbuyBill/importData",
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询请购单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeDRequest && "" != this.daterangeDRequest) {
        this.queryParams.params["beginDRequest"] = this.daterangeDRequest[0];
        this.queryParams.params["endDRequest"] = this.daterangeDRequest[1];
      }
      listBill(this.queryParams).then((response) => {
        this.billList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        vBillNo: null,
        applyBy: null,
        oprBy: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        dRequest: null,
        vPartNo: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDRequest = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加请购单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBill(id).then((response) => {
        this.form = response.data.billPO;
        this.itemList = response.data.itemPOList;
        this.open = true;
        this.title = "请购单详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBill(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBill(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "请购单导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "buy/buy0003/importTemplate",
        {},
        `合同明细_${new Date().getTime()}.xlsx`
      );
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      console.log("--------------------------------");
      console.log(response);
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  },
};
</script>
<style>
.xin-main {
  position: relative;
  height: 100%;
  overflow: hidden;
}
.xin-content {
  overflow-y: scroll;
  box-sizing: border-box;
  overflow-x: hidden;
}
</style>
